// pages/resume/fulltime/detail.js
const config = getApp().globalData.config
const T = getApp().globalData.T
import { collect, isCollect} from '../../../utils/collect.js'
let token = ''
let userId = -1;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    resumeId: -1,
    collectStatus: 0,
    id: -1, // 简历用户id
    status:-1,//简历状态
    resumeDeliverId:-1,//面试id
    noOrder:true,//没有预约面试
    orderData: {
        }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log('onLoad:' + this.data.status);
    this.setData({
      status: options.status,
      resumeId: options.resumeId,// 简历id
      id: options.id,//用户id
      resumeDeliverId: options.resumeDeliverId,//面试id
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    console.log('status:' + this.data.status);
    token = getApp().globalData.token;
    userId = getApp().globalData.userId;
    this.getDetail();
    this.getCollectStatus();
    if(this.data.resumeDeliverId){
      this.getOrderData();
    }
  },

  getDetail() {
    T.req({
      loadTitle: '加载中...',
      url: config.api + 'resume/full/detail/' + this.data.id,
      method: 'GET',
      data: {
        token: token,
        resumeDeliverId:this.data.resumeDeliverId || null
      },
      success: (res) => {
        this.setData({
          obj: res,
          resumeId: res.resumeId
        })
      }
    })
  },

  getCollectStatus() {
    isCollect(this.data.resumeId, 3).then(res => {
      this.setData({
        collectStatus: res
      })
    })
  },

getOrderData(){
  T.req({
    loadTitle: '加载中...',
    url: config.api + 'interview',
    method: 'GET',
    data: {
      token: token,
      resumeDeliverId: this.data.resumeDeliverId
    },
    success: (res) => {
      if (res.id == 0){
        this.setData({
          noOrder: true
        })
      }else{
        this.setData({
          noOrder:false,
          orderData:res
        })
      }
    }
  })
},
  /**
   * 预约面试
   */
  onOrder() {
    let obj = { 
      resumeDeliverId: this.data.resumeDeliverId,
      candidateId: this.data.id, 
      resumeId: this.data.resumeId,
      type: 0
    }
    wx.navigateTo({
      url: '../order/order?' + T.param(obj),
    })  
  },

  onCollect() {
    let _param = {
      token: token,
      userId: userId,
      type: 3,
      relatedId: this.data.resumeId
    }
    collect(_param).then((res) => {
      T.toast({
        id: '#toast',
        text: this.data.collectStatus ? '取消成功' : '收藏成功'
      })
      this.setData({
        collectStatus: 1 ^ this.data.collectStatus
      })
    })
  }
})